package com.tcm.doctor.web.controller;

import com.base.core.exception.ServiceException;
import com.base.core.pager.PageBean;
import com.base.core.utils.R;
import com.tcm.prescribe.api.domain.dto.PrescribeDTO;
import com.tcm.prescribe.api.service.PrescribeService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;


@Slf4j
@Api(value = "prescribe", tags = "开方接口")
@RequestMapping("/doctor")
@RestController
public class PrescribeController {

    @DubboReference(version = "0.0.1-alpha", timeout = 6000, check = false, retries = 3)
    private PrescribeService prescribeService;

    /**
     * 根据医生账号id，搜索关键词查询历史方案列表
     * @param
     * @return
     * @throws Exception
     */
    @GetMapping("/prescribe/list/page")
    public R listPage(@RequestParam Long doctorId,
                      @RequestParam(defaultValue = "1") Long page,
                      @RequestParam(defaultValue = "10") Long pagesize) throws Exception {
        PageBean<PrescribeDTO> prescribeDTOPageBean = prescribeService.listDoctorPrescribePage(
                doctorId, page, pagesize);
        return R.success(prescribeDTOPageBean);
    }

    @GetMapping("/prescribe/detail")
    public R form(@RequestParam Long id){
        PrescribeDTO prescribeForm = prescribeService.findById(id);
        return R.success(prescribeForm);
    }

    /**
     * 新增方案
     * @param prescribeDto
     * @return
     */
    @PostMapping("/prescribe/save")
    public R save(@RequestBody PrescribeDTO prescribeDto) {
        try{
           Long id = prescribeService.save(prescribeDto);
           return R.success(id);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * 更新方案
     * @param prescribeDto
     * @return
     */
    @PostMapping("/prescribe/update")
    public R update(@RequestBody PrescribeDTO prescribeDto) {
        try{
            prescribeService.update(prescribeDto);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/prescribe/delete")
    public R delete(@RequestParam Long id) {
        try{
            prescribeService.deleteLogicById(id);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

}
